#include "raylib.h"
#include <math.h>
#include <stdio.h>
#include <stdint.h>

#define WinH 600
#define WinW 800

#define TANK 1
#define PAWN 0
#define TEAM_A 4
#define TEAM_B 8

typedef struct Vec2IntStruct{
  int x;
  int y;
}Vec2Int;

typedef struct pieceInfoStruct{
  uint8_t atk_off : 2;
  uint8_t def_off : 2;
  uint8_t type : 1;
  uint8_t team : 1;
  uint8_t hp : 1;
  uint8_t Null : 1;
}pieceInfo;

const Vec2Int PawnDefault = {1,2};
const Vec2Int TankDefault = {2,3};

pieceInfo map [6][4] ; //[x][y]
Texture PAWN_SIGN;
Texture TANK_SIGN;
RenderTexture BufferRT;
int WindowHeight;
int WindowWidth;

Texture LoadTextureEx(char * filename){
  Image Temp = LoadImage(filename);
  Texture ReturnT = LoadTextureFromImage(Temp);
  UnloadImage(Temp);
  return ReturnT;
}

pieceInfo * NewPiece(int type,int atk, int def, Vec2Int pos, int team){
  Vec2Int Default = type == PAWN ? PawnDefault : TankDefault;
  pieceInfo NewP = {atk - Default.x,def - Default.y,type,team,1,0};
  map[pos.x][pos.y] = NewP;
  return &(map[pos.x][pos.y]);
}

void InitMap(){
  for(int i = 0;i < 6;i++){
    for (int j = 0; j < 4; j++){
      map[i][j] = (pieceInfo){0,0,0,0,0,1};
    }
  }
}

void DrawPiece(pieceInfo piece, Vec2Int pos){
  Vec2Int Default = piece.type == PAWN ? PawnDefault : TankDefault;
  Vector2 offsetFirst = {200,200};
  Vector2 offsetAll = {50,50};
  Vector2 offsetText = {20,0};
  
  Color TextColor = piece.team == 0 ? BLACK : RAYWHITE;
  Color ImageColor = piece.hp == 1 ? RAYWHITE : RED;
  DrawTexture(piece.type == PAWN ? PAWN_SIGN : TANK_SIGN ,
              pos.x*offsetAll.x + offsetFirst.x,
              pos.y * offsetAll.y + offsetFirst.y,
              ImageColor);
  char temp[20];
  sprintf(temp,"%d   %d",piece.atk_off + Default.x,piece.def_off + Default.y);
  DrawText(temp,
           pos.x*offsetAll.x + offsetFirst.x + offsetText.x,
           pos.y * offsetAll.y + offsetFirst.y + offsetText.y,
           24,
           TextColor);
}

void DrawMap(){
  for (int i = 0;i < 6;i++){
    for (int j = 0;j < 4; j++){
      if (map[i][j].Null != 1){
        DrawPiece(map[i][j],(Vec2Int){i,j});
      }
    }
  }
}

int main(){
  InitWindow(WinW, WinH, "W");

  InitMap();
  PAWN_SIGN = LoadTextureEx("./img/pawn.png");
  BufferRT = LoadRenderTexture(WinW, WinH);
  
  NewPiece(PAWN, 2, 2, (Vec2Int){2,2}, 1);
  
  while (!WindowShouldClose()){
    int WindowWidth = GetRenderWidth();
    int WindowHeight = GetRenderHeight();
    
    // 渲染到纹理
    BeginTextureMode(BufferRT);
    ClearBackground(BROWN);
    DrawMap();
    EndTextureMode();
    
    // 渲染到屏幕
    BeginDrawing();
    ClearBackground(BLACK);
        
    // 计算最小缩放比例，保持宽高比
    float scaleX = (float)WindowWidth / WinW;
    float scaleY = (float)WindowHeight / WinH;
    float minScale = fminf(scaleX, scaleY); // 最小缩放，保持内容完整
    
    // 计算居中位置
    float scaledWidth = WinW * minScale;
    float scaledHeight = WinH * minScale;
    float offsetX = (WindowWidth - scaledWidth) / 2.0f;
    float offsetY = (WindowHeight - scaledHeight) / 2.0f;
        
    // 使用DrawTexturePro进行缩放和定位
    Rectangle sourceRec = { 0, 0, (float)WinW, -(float)WinH };
    Rectangle destRec = { offsetX, offsetY, scaledWidth, scaledHeight };
    Vector2 origin = { 0, 0 };
    
    DrawTexturePro(BufferRT.texture, sourceRec, destRec, origin, 0, WHITE);
    EndDrawing();
  }
}
